﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.Persistent;
using Jumpton.SFA.Server.BusinessRule.CallRoute;

public partial class CallRoute_CPR_List_Product_View : PageBase
{
    private decimal id = 0;
    private const string CHANNEL_ID = "id";
    protected string Name = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        gvCPR.EnableViewState = false;

        if (Request.QueryString[CHANNEL_ID] != null)
        {
            decimal.TryParse(Request.QueryString[CHANNEL_ID], out id);
        }
        Name = Request.QueryString["name"];
        if (!IsPostBack)
        {
            DataBind();
            BindTypeOne();
        }
    }

    /// <summary>
    /// 一级
    /// </summary>
    private void BindTypeOne()
    {
        this.TreeView.Nodes.Clear();
        ObjectTable table = new ManufacturersManager().GetManufacturersTable(MyStateManager.DomainID);

        for (int i = 0; i < table.Count; i++)
        {
            MANUFACTURERS Dict = (MANUFACTURERS)table[i];
            TreeNode node = new TreeNode(Dict.ACTIVE ? Dict.ManuName : Dict.ManuName + "-无效", Dict.ManuCode);
            TreeView.Nodes.Add(node);
        }

        TreeView.ExpandDepth = 1;
        TreeView.DataBind();
    }

    /// <summary>
    /// 二级
    /// </summary>
    /// <param name="parentNode"></param>
    /// <param name="parentId"></param>
    private void BindTypeTwo(TreeNode parentNode)
    {
        ObjectTable table = CategoryManager.GetCategories(MyStateManager.DomainID);

        for (int i = 0; i < table.Count; i++)
        {
            CATEGORIES cate = (CATEGORIES)table[i];

            TreeNode node = new TreeNode(cate.ACTIVE ? cate.CategoryName : cate.CategoryName, cate.CategoryCode);
            parentNode.ChildNodes.Add(node);
        }
    }

    /// <summary>
    //  三级
    /// </summary>
    /// <param name="parentNode"></param>
    /// <param name="parentId"></param>
    private void BindTypeThree(TreeNode parentNode)
    {
        ObjectTable table = BrandManager.GetBrandwithCategory(parentNode.Value,parentNode.Parent.Value, MyStateManager.DomainID);
        for (int i = 0; i < table.Count; i++)
        {
            BRANDS brand = (BRANDS)table[i];
            TreeNode node = new TreeNode(brand.IsValid ? brand.BrandName : brand.BrandName, brand.BrandCode);
            parentNode.ChildNodes.Add(node);
        }
    }

    /// <summary>
    //  四级
    /// </summary>
    /// <param name="parentNode"></param>
    /// <param name="parentId"></param>
    private void BindTypeFour(TreeNode parentNode)
    {
        ObjectTable table = Jumpton.SFA.Server.BusinessRule.MasterData.ProductManager.GetProductwithBrand(parentNode.Value, MyStateManager.DomainID);

        for (int i = 0; i < table.Count; i++)
        {
            PRODUCTS product = (PRODUCTS)table[i];
            TreeNode node = new TreeNode(product.ACTIVE ? product.ProductName : product.ProductName, product.ID.ToString());

            parentNode.ChildNodes.Add(node);
        }
    }

    protected void TreeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (TreeView.SelectedNode.ChildNodes.Count == 0)
        {
            switch (TreeView.SelectedNode.Depth)
            {
                case 0:
                    {
                        BindTypeTwo(TreeView.SelectedNode);
                        break;
                    }
                case 1:
                    {

                        BindTypeThree(TreeView.SelectedNode);
                        break;
                    }
                case 2:
                    {
                        BindTypeFour(TreeView.SelectedNode);
                        break;
                    }
                case 3:
                    {
                        DataSet ds = RouteManager.SelectCustomerProduct(id, decimal.Parse(TreeView.SelectedNode.Value), MyStateManager.DomainID, 6);

                        if (ds != null && ds.Tables.Count > 0)
                        {
                            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
                            {
                                BoundField nameBoundField = new BoundField();
                                nameBoundField.DataField = ds.Tables[1].Rows[i]["FieldName"].ToString();
                                nameBoundField.HeaderText = ds.Tables[1].Rows[i]["MeasurementName"].ToString();
                                gvCPR.Columns.Add(nameBoundField);
                            }
                            gvCPR.DataSource = ds.Tables[0];
                            gvCPR.DataBind();
                        }
                        else
                        {
                            gvCPR.DataSource = null;
                            gvCPR.DataBind();
                        }


                        break;
                    }

            }
        }

    }


}
